//index.js
//获取应用实例
var app = getApp()
Page({
	data: {
		root:'',
		siteroot:app.setting.siteroot,
		att_url:app.setting.att_url,
		screenHeight:app.screenHeight,
		nowimgindex:0,
		action:'goods.detail',
		pagetitle:'详情' + '-' + app.setting.title,
		money:10,
	},
	onReady: function (){
		//获得粉丝消息弹窗(需要时开启)
		this.ShowAuthMD = this.selectComponent("#ShowAuthM");
		this.showsharecoverMD = this.selectComponent("#showsharecover");
		this.sendlipingMD = this.selectComponent("#sendliping");
		this.showhongbaoMD = this.selectComponent("#showhongbao");
		this.chaihbMD = this.selectComponent("#chaihb");
		this.loadingToastMD = this.selectComponent("#loadingToast");
		this.is_yanzhengMD = this.selectComponent("#is_yanzheng");
		var fansinfo = wx.getStorageSync('userInfo');
		if(!fansinfo){
			// this.ShowAuthM.show();
		}
	},
	onLoad: function (opt){
		var page=this;
		wx.setNavigationBarTitle({'title':page.data.pagetitle});
		var route=page.route;
		page.setData({
			route:route,
		});
		if(opt.fromuser){
			var path=route.slice(0,-6).split('/');
			app.access.plugin=path.shift();
			app.access.action=path.join('.');
			app.access.query =app.util.made_query(opt,true);
			app.access.op=opt.op?opt.op:'';
		}
		app.util.get(app.access,function(pagedata){
			console.log('pagedata');console.log(pagedata);
			page.setData({
				pagedata:pagedata,
				action:app.access.action
			});
		});
	},
	info_login:function(e){
		//根据openid为用户登陆，此处尝试获取用户信息
		app.util.getUserInfo(e);
		this.ShowAuthM.hide();
	},
  	click:function(evt){
		//用户点击事件 data.func 为方法名，data.params 为调用参数！
		var data=evt.currentTarget.dataset;
		app.util[data.func](data.params);
	},
	formSubmit:function(event){
		var data=event.detail.value;
		app.util.post('post',data);
	},
	showzufu: function () {
		var page = this
		var showzufu = page.data.showzufu
		page.setData({
			showzufu: !showzufu
		});
	},
	sendliping: function () {
		var page = this
		var sendliping = page.data.sendliping
		if (sendliping) {
			this.sendlipingMD.hide();
		} else {
			this.sendlipingMD.show();
		}
		page.setData({
			sendliping: !sendliping
		});
	},
	is_yanzheng:function(){
		var the = this
		var is_yanzheng = the.data.is_yanzheng
		if(is_yanzheng){
			the.is_yanzhengMD.hide()
		}else{
			the.code_image()
			the.is_yanzhengMD.show()
		}
		the.setData({
			is_yanzhengMD:!is_yanzhengMD
		})
	},
	showhongbao: function (e) {
		var page = this
		var isshow = e.currentTarget.dataset.isshow;
		if (isshow==0) {
			this.showhongbaoMD.hide();
		} else {
			this.showhongbaoMD.show();
		}
		page.setData({
			showhongbao: isshow
		});
	},
	chaihb: function () {
		var page = this
		var chaihb = page.data.chaihb
		if (chaihb) {
			this.chaihbMD.hide();
		} else {
			this.chaihbMD.show();
		}
		page.setData({
			chaihb: !chaihb
		});
	},
	loadingToast: function () {
		var page = this
		var loadingToast = page.data.loadingToast
		if (loadingToast) {
		  this.loadingToastMD.hide();
		} else {
		  this.loadingToastMD.show();
		}
		page.setData({
			loadingToast: !loadingToast
		});
	},
	bindtouchstart: function (e) {
		var the = this
		var pagedata = the.data.pagedata
		var data=e.changedTouches[0]
		the.setData({
			pageYstart: data.pageY
		});
	},
	bindtouchend: function (e) {
		var the = this
		var pagedata = the.data.pagedata
		var nowimgindex=the.data.nowimgindex
		var data=e.changedTouches[0]
		var pageYstart=the.data.pageYstart
		if(data.pageY>pageYstart && data.pageY-pageYstart>60){
			if(!pagedata.goods.video && nowimgindex+1<pagedata.goods.images.length){
				nowimgindex++
				the.setData({
					nowimgindex: nowimgindex
				});
			}else{
				the.getitem();
			}
			
		}else if(data.pageY<pageYstart && pageYstart-data.pageY>60){
			if(!pagedata.goods.video && nowimgindex>0){
				nowimgindex--
				the.setData({
					nowimgindex: nowimgindex
				});
			}else{
				the.getitem(1);
			}
		}
	},
	getitem: function(type){
		var the = this
		var pagedata = the.data.pagedata
		if(type==1){
			var goodsid=pagedata.onid
		}else{
			var goodsid=pagedata.nextid
		}
		app.util.get('jy_weishop/video.detail//id='+goodsid,function(res){
			the.setData({
				pagedata: res
			});
		});
	},
	setmoney: function (e) {
		var page = this
		var money = e.currentTarget.dataset.num;
		var pagedata = page.data.pagedata
		pagedata.post.money = money
		page.setData({
			pagedata: pagedata
		});
	},
	setshoucang: function () {
		var page = this
		var pagedata = page.data.pagedata
		pagedata.goods.islike = pagedata.goods.islike ? 0 : 1;
		app.util.post('like', '', function (res) {
			app.util.msg(res);
			page.setData({
				pagedata: pagedata
			});
		});
	},
	username:function(e){
		var the = this
		the.setData({
			'pagedata.post.username':e.detail.value
		})
	},
	clearNoNum:function(obj){
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除"数字"和"."以外的字符  
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是  
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的  
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
		obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数  
	},
	//获取投票
	vote: function () {
		var page = this
		var pagedata = page.data.pagedata
		var data = { 'vote': pagedata.goods.isvote }
		pagedata.goods.isvote = pagedata.goods.isvote ? 0 : 1;
		app.util.post("vote",data, function (res) {
				app.util.msg(res);
				if(pagedata.goods.isvote==1){
					pagedata.goods.likes++
				}else{
					pagedata.goods.likes--
				}
				page.setData({
					pagedata: pagedata
				});
				var time = 0
				for (var i = 0; i < 88; i++) {
					// setTimeout(page.likepiaofu, time);
					if (i < 10) {
						time += 100
					} else if (i >= 10 && i < 20) {
						time += 200
					} else if (i >= 20 && i < 30) {
						time += 300
					} else if (i >= 30 && i < 40) {
						time += 400
					} else if (i >= 40 && i < 50) {
						time += 500
					} else if (i >= 50 && i < 60) {
						time += 600
					} else if (i >= 60 && i < 70) {
						time += 700
					} else if (i >= 70 && i < 80) {
						time += 800
					} else {
						time += 900
					}
				}
		})
	},
	//获取关注
	setfollow: function () {
		var page = this
		var pagedata = page.data.pagedata
		pagedata.goods.islike = pagedata.goods.islike ? 0 : 1;
		app.util.post("like", '', function (res) {
			app.util.msg(res);
			page.setData({
					pagedata: pagedata
			});
			var time = 0
			for (var i = 0; i < 88; i++) {
				// setTimeout(page.followpiaofu, time);
				if (i < 10) {
					time += 100
				} else if (i >= 10 && i < 20) {
					time += 200
				} else if (i >= 20 && i < 30) {
					time += 300
				} else if (i >= 30 && i < 40) {
					time += 400
				} else if (i >= 40 && i < 50) {
					time += 500
				} else if (i >= 50 && i < 60) {
					time += 600
				} else if (i >= 60 && i < 70) {
					time += 700
				} else if (i >= 70 && i < 80) {
					time += 800
				} else {
					time += 900
				}
			}
		})
	},
	  // //收藏
	  // setshoucang: function () {
	  //   var page = this
	  //   var pagedata = page.data.pagedata
	  //   var data = { 'isshoucang': pagedata.isshoucang }
	  //   pagedata.isshoucang = pagedata.isshoucang ? 0 : 1;
	  //   app.util.post("shoucang",data,function (res) {
	  //     wx.showToast({
	  //       title: res,
	  //     })
	  //     })
	  // },
	//获取留言
	getmsg: function () {
		var page = this
		var pagedata = page.data.pagedata
		pagedata.post.msgshow = 1
		app.util.get("getmsg",function (res) {
		})
	},
	//提交留言
	setmsg: function (e) {
		var page = this
		var msg = e.currentTarget.dataset.msg
		var pagedata = page.data.pagedata
		var data = { 'msg': msg }
		page.showzufu()
		pagedata.islike = 1
		app.util.post('setmsg',data,function (res) {
			wx.showToast({
				title: res.msg,
			})
        // vote_tanmu(msg, res.avatar)
		})
	},
	//提交礼物
	sendlp: function (e) {
		var page = this
		var pagedata = page.data.pagedata
		var value = e.currentTarget.dataset
		var data = { 'lpid': value.id }
		app.util.post('sendlp',data, function (res) {
			console.log('res.payid');
			console.log(res.payid);
			if (res.payid) {
				app.util.pay(res.payid, function (res) {
					if (res.type == 'success') {
						wx.showToast({
							title: '支付成功！',
						})
					} else {
						wx.showToast({
							title: '支付失败！失败原因：' + res.message,
							icon: 'error',
						})
					}
				});
			}

		})
	},
	//投票
	setvote:function(e){
		var the = this
		var pagedata = the.data.pagedata
		var value = e.currentTarget.dataset
		if(pagedata.isfollow==0 && (pagedata.reply.isfollow=='1' || pagedata.reply.isfollow=='3')){
			wx.showToast({
				'title':'请先关注，再投票吧！',
				'success':function(){
				},
				'fail':function(){}
			});
			return false
		}
		if(!value.type){
			the.code_image()
		}else{
			the.is_yanzhengMD.hide()
		}
		if(!value.type && pagedata.reply.is_yanzheng =='1'){
			wx.showToast({
				'title':'请先输入验证码！',
				'success':function(){
					the.is_yanzheng();
				},
				'fail':function(){}
			});
			return false
		}
		if(value.type && pagedata.reply.is_yanzheng =='1' && !pagedata.post.code){
			app.util.message('请先输入验证码!');
			return false
		}
		app.util.post('setvote',{'code':pagedata.post.code},function(res){
			pagedata.post.code='';
			if(res){
				app.util.message('投票成功!')
				if(res.istanchu ==1){
					setTimeout(function(){
						the.chaihbMD.show()
					},1000);
				}
			}else{
				app.util.message(res)
				the.chaihbMD.show()
			}
		})
	},
	code_image:function(){
		var the = this
		var url=the.data.siteroot+'api.php?this=custom&token='+app.token+'&plugin=core&action=tools.code&v='+Math.ceil(Math.random() * 10000);
		the.setData({
			url:url
		})
	},
	code:function(e){
		var the = this
		the.setData({
			'pagedata.post.code':e.detail.value
		})
	},
	showspec:function(){
		var page=this
		var showspec = page.data.showspec
		page.setData({
				showspec:!showspec
		});
	},
	minus:function(){
		var page=this
		var pagedata = page.data.pagedata
		if(pagedata.post.num <=1){
			app.util.msg('不能再减了','error');
		}else{
			pagedata.post.num--;
			page.setData({
				pagedata:pagedata
			});
		}
		
	},
	plus:function(){
		var page=this
		var pagedata = page.data.pagedata
		pagedata.post.num++;
		if(pagedata.goods.usermaxbuy>0 && pagedata.goods.order_goodscount + pagedata.post.num>=pagedata.goods.usermaxbuy){
			app.util.msg('不能再加了，超过该商品最大购买量！','error');
			return false;
		}
		if(pagedata.goods.maxbuy>0 && pagedata.post.num>=pagedata.goods.maxbuy){
			app.util.msg('不能再加了，超过该商品单次购买量！','error');
			return false;
		}
		if(pagedata.post.num>=pagedata.options.stock){
			app.util.msg('不能再加了，超了库存啦！','error');
			return false;
		}
			page.setData({
				pagedata:pagedata
			});
	},
	addtocart:function(){
		var the=this
		var pagedata = the.data.pagedata
		pagedata.cartCount ++;
		the.setData({
				pagedata:pagedata,
				showspec:false,
		});
		app.util.post('jy_weishop/goods.detail/addtocart/',{'goodsid':pagedata.post.goodsid,'num':pagedata.post.num,'specid':pagedata.post.specid});
	},
	get_optionsid:function(e){
		var page=this
		var k=e.currentTarget.dataset.key;
		var item = e.currentTarget.dataset.item;
		var pagedata = page.data.pagedata
		pagedata.post.specid[k] = item;
		var specid = pagedata.post.specid;
		specid = specid.join('_');
		app.util.post('jy_weishop/goods.detail/get_optionsid/',{'specid':specid},function(res){
			pagedata.post.optionsid=res.options.id;
			pagedata.options=res.options;
			pagedata.goods.price=res.options.price;
			pagedata.goods.originalprice=res.options.productprice;
			pagedata.selespename=res.selespename;
			page.setData({
				pagedata:pagedata
			});
		});
		
	},
	check_buy:function(){
		var pagedata=this.data.pagedata
		var goodsid=pagedata.post.goodsid
		var num=pagedata.post.num
		var optionsid=pagedata.post.optionsid
		var addressid=pagedata.post.addressid
		var storeid=pagedata.post.storeid
		app.util.get('jy_weishop/goods.detail/check_buy/goodsid='+goodsid+'&num='+num+'&optionsid='+optionsid+'&addressid='+addressid+'&storeid='+storeid);
	},
	onShareAppMessage: function (res) {
		var the = this
		var pagedata = the.data.pagedata
		if (res.from === 'button') {
		}
		var path=the.data.route+'?'+app.access.query+'&id='+pagedata.goods.id;
		if(app.access.op){
			path +='&op='+app.access.op;
		}
		if(pagedata.fromuser){
			path +='&fromuser='+pagedata.fromuser;
		}
		return {
		  title: the.data.pagetitle,
		  path: path,
		  desc: app.setting.sharedesc,
		  success: function(res) {
		  },
		  fail: function(res) {
		  }
		}
	}
});